package cn.rxls.community.controller;

import cn.rxls.community.dto.maintenance.MaintenanceAddDTO;
import cn.rxls.community.dto.maintenance.MaintenancePageDTO;
import cn.rxls.community.dto.maintenance.MaintenanceUpdateDTO;
import cn.rxls.community.result.Result;
import cn.rxls.community.service.MaintenanceService;
import cn.rxls.community.vo.maintenance.MaintenanceVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * 维修(Maintenance)表
 */
@RestController
@RequestMapping("maintenance")
@Validated
public class MaintenanceController {
    /**
     * 服务对象
     */
    @Resource
    private MaintenanceService maintenanceService;


    /**
     * 分页查询维修内容
     *
     * @param dto 分页对象
     * @return 多条维修信息
     */
    @GetMapping("/page")
    public Result<List<MaintenanceVO>> selectPageByState(@ModelAttribute @Valid MaintenancePageDTO dto) {
        return Result.success(maintenanceService.selectPageByState(dto));
    }


    /**
     * 根据维修id查询维修内容
     *
     * @param id 维修id
     * @return 单条维修内容数据
     */
    @GetMapping("/detail")
    public Result<MaintenanceVO> selectDetail(@RequestParam(value = "id", required = false)
                                              @NotNull(message = "维修编号不为空")
                                              Long id) {
        return Result.success(maintenanceService.selectDetail(id));
    }

    /**
     * 维修新增
     *
     * @param dto 参数对象
     * @return 是否成功
     */
    @PostMapping("/add")
    public Result<Boolean> insertMaintenance(@Valid @RequestBody MaintenanceAddDTO dto) {
        return Result.success(maintenanceService.insertMaintenance(dto));
    }


    /**
     * 维修内容变更
     *
     * @param dto 参数对象
     * @return 是否成功
     */
    @PostMapping("/update")
    public Result<Boolean> updateMaintenance(@Valid @RequestBody MaintenanceUpdateDTO dto) {
        return Result.success(maintenanceService.updateMaintenance(dto));
    }


    /**
     * 根据分类id获取维修分类类目
     * @param id 分类id
     * @return 多条维修分类信息
     */
    @GetMapping("/category")
    public Result<?> selectMaintenanceCategoryById(@RequestParam("id")
                                                   @NotNull(message = "分类不为空")
                                                   Long id) {

        return Result.success(maintenanceService.selectMaintenanceCategoryById(id));
    }

}

